package com.bjpower.springboot.preparedstatement.crud;

import com.bjpower.springboot.bean.Employee;
import com.bjpower.springboot.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * @Author: yg
 * @create 2025-01-24 - 16:05
 * @
 */
public class EmployeesForQuery {
//    employee_id, first_name, last_name,eamil,
//    phone_number, hire_date, job_id, salary,
//    commission_pct, manager_id, department_id
    @Test
    public void testQueryForEmployees(){
        String sql = "select employee_id, first_name, email, hire_date, salary from employees where employee_id = ?";
        Employee employee = queryForEmployees(sql, 101);
        System.out.println(employee);
    }

    public Employee queryForEmployees(String sql, Object ...args){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i+1, args[i]);
            }
            rs = ps.executeQuery();
            //获取结果集元数据：ResultSetMetaData
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if (rs.next()){
                Employee cust = new Employee();
                for (int i = 0; i < columnCount; i++) {
                    //获取列植
                    Object columnValue = rs.getObject(i+1);
                    //获取列名
                    String columnName = rsmd.getColumnName(i+1);
                    //给cust对象制定的columnName属性，赋值给columnValue,通过反射
                    Field field = Employee.class.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(cust, columnValue);
                }
                return cust;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);
        }
        return null;
    }

    @Test
    public void testQuery1(){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select employee_id, first_name, email, hire_date, salary" +
                    "from employees where employee_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, 100);
            resultSet = ps.executeQuery();
            System.out.println(resultSet);
            if (resultSet.next()){
                int employee_id = resultSet.getInt(1);
                String first_name =  resultSet.getString(2);
                String email =  resultSet.getString(3);
                Date hire_date = resultSet.getDate(4);
                Double salary = resultSet.getDouble(5);
                Employee employee = new Employee(employee_id,first_name,email,hire_date,salary);
                System.out.println(employee);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.closeResource(conn, ps, resultSet);
        }
    }
}
